import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
@Injectable({
  providedIn: 'root'
})
export class CustomerService {
  data = '/Customer'
  constructor(private http: HttpClient) { }
  private createPaginationParams(currentPage: number, itemsPerPage: number): HttpParams {
    return new HttpParams()
      .set('pageNumber', currentPage.toString())
      .set('pageSize', itemsPerPage.toString());
  }// 创建分页参数
  GetPagedCustomerClassWithSub(currentPage: number, itemsPerPage: number){
    const GetPagedCustomerClassWithSub = '/GetPagedCustomerClassWithSub';
    const params = this.createPaginationParams(currentPage, itemsPerPage);
    return this.http.get(environment.apiUrl+ this.data+GetPagedCustomerClassWithSub,{params});
  }// 获取所有客户分类
  CreateCustomerClass(data:any){
    const CreateCustomerClass = '/CreateCustomerClass';
    return this.http.post(environment.apiUrl+ this.data+CreateCustomerClass,data);
  }// 创建客户分类
  UpdateCustomerClass(id:any,data:any){
    const UpdateCustomerClass = '/UpdateCustomerClass';
    return this.http.put(environment.apiUrl+ this.data+UpdateCustomerClass,data,{params:{id}});
  }// 修改客户分类
  DeleteCustomerClass(id:any){
    const DeleteCustomerClass = '/DeleteCustomerClass';
    return this.http.put(environment.apiUrl+ this.data+DeleteCustomerClass,null,{params:{id}});
  }// 删除客户分类
  GetPagedCustomerDiscount(currentPage: number, itemsPerPage: number){
    const GetPagedCustomerDiscount = '/GetPagedCustomerDiscount';
    const params = this.createPaginationParams(currentPage, itemsPerPage);
    return this.http.get(environment.apiUrl+ this.data+GetPagedCustomerDiscount,{params});
  }// 获取所有客户折扣
  CreateCustomerDiscount(data:any){
    const CreateCustomerDiscount = '/CreateCustomerDiscount';
    return this.http.post(environment.apiUrl+ this.data+CreateCustomerDiscount,data);
  }// 创建客户折扣
  DeleteCustomerDiscount(id:any){
    const DeleteCustomerDiscount = '/DeleteCustomerDiscount';
    return this.http.put(environment.apiUrl+ this.data+DeleteCustomerDiscount,null,{params:{id}});
  }// 删除客户折扣
  UpdateCustomerDiscount(id:any,data:any){
    const UpdateCustomerDiscount = '/UpdateCustomerDiscount';
    return this.http.put(environment.apiUrl+ this.data+UpdateCustomerDiscount,data,{params:{id}});
  }// 修改客户折扣

  CreateCustomer(data:any){
    const CreateCustomer = '/CreateCustomer';
    return this.http.post(environment.apiUrl+ this.data+CreateCustomer,data);
  }
  GetPagedCustomer(filter:{
    PageSize?:number,
    PageNumber?:number,
    SearchTerm?:string,
    SearchStatus?:string,
    SearchCustomerType?:string,
    SalesmanId?:any,
    CustomerClassId?:any,
  }){
    const filteredFilter = Object.keys(filter)
        .filter(key => filter[key] !== null && filter[key] !== 0 && filter[key] !== '')
        .reduce((obj, key) => {
            obj[key] = filter[key];
            return obj;
        }, {});

    const GetPagedCustomer = '/GetPagedCustomer';
    return this.http.get(environment.apiUrl+ this.data+GetPagedCustomer,{params:filteredFilter});
  }
  DownloadCustomerClassFileModel() {
    const endpoint = '/DownloadCustomerClassFileModel';
    return this.http.get(environment.apiUrl + '/Excel' + endpoint, { responseType: 'blob' });
  }
  DownloadCustomerFileModel() {
    const endpoint = '/DownloadCustomerFileModel';
    return this.http.get(environment.apiUrl + '/Excel' + endpoint, { responseType: 'blob' });
  }
  importCustomerClassFromExcel(file: File) {
    const formData: FormData = new FormData();
    formData.append('file', file, file.name);
    const importCustomerClassFromExcelEndpoint = '/ImportCustomerClassFromExcel';
    return this.http.post(environment.apiUrl + '/Excel' + importCustomerClassFromExcelEndpoint, formData, { responseType: 'blob'
    });
  }
  importCustomerFromExcel(file: File) {
    const formData: FormData = new FormData();
    formData.append('file', file, file.name);
    const importCustomerFromExcelEndpoint = '/ImportCustomerFromExcel';
    return this.http.post(environment.apiUrl + '/Excel' + importCustomerFromExcelEndpoint, formData, { responseType: 'blob'
    });
  }
  GetCustomerById(id:any){
    const GetCustomerById = '/GetCustomerById';
    return this.http.get(environment.apiUrl+ this.data+GetCustomerById,{params:{id}});
  }
  UpdateCustomer(id:any,data:any){
    const UpdateCustomer = '/UpdateCustomer';
    return this.http.put(environment.apiUrl+ this.data+UpdateCustomer,data,{params:{id}});
  }
  GetPagedCurrency(currentPage: number, itemsPerPage: number){
    const GetPagedCurrency = '/GetPagedCurrency';
    const params = this.createPaginationParams(currentPage, itemsPerPage);
    return this.http.get(environment.apiUrl+ this.data+GetPagedCurrency,{params});
  }
  GetPagedCurrencyF(filter:{
    PageSize?:number,
    PageNumber?:number,
    Num?:string,
    Name?:string,
    SearchStatus?:string,
  }){
    const GetPagedCurrency = '/GetPagedCurrency';
    return this.http.get(environment.apiUrl+ this.data+GetPagedCurrency,{params:filter});
  }
  CreateCurrency(data:any){
    const CreateCurrency = '/CreateCurrency';
    return this.http.post(environment.apiUrl+ this.data+CreateCurrency,data);
  }
  GetCurrencyById(id:any){
    const GetCurrencyById = '/GetCurrencyById';
    return this.http.get(environment.apiUrl+ this.data+GetCurrencyById,{params:{id}});
  }
  UpdateCurrency(id:any,data:any){
    const UpdateCurrency = '/UpdateCurrency';
    return this.http.put(environment.apiUrl+ this.data+UpdateCurrency,data,{params:{id}});
  }
  DeleteCurrency(id:any){
    const DeleteCurrency = '/DeleteCurrency';
    return this.http.put(environment.apiUrl+ this.data+DeleteCurrency,null,{params:{id}});
  }
  GetPagedSettlementMethodF(filter:{
    PageSize?:number,
    PageNumber?:number,
    Num?:string,
    Name?:string,
    SearchStatus?:string,
  }){
    const GetPagedSettlementMethod = '/GetPagedSettlementMethod';
    return this.http.get(environment.apiUrl+ this.data+GetPagedSettlementMethod,{params:filter});
  }
  GetPagedSettlementMethod(currentPage: number, itemsPerPage: number){
    const GetPagedSettlementMethod = '/GetPagedSettlementMethod';
    const params = this.createPaginationParams(currentPage, itemsPerPage);
    return this.http.get(environment.apiUrl+ this.data+GetPagedSettlementMethod,{params});
  }
  GetSettlementMethodById(id:any){
    const GetSettlementMethodById = '/GetSettlementMethodById';
    return this.http.get(environment.apiUrl+ this.data+GetSettlementMethodById,{params:{id}});
  }
  CreateSettlementMethod(data:any){
    const CreateSettlementMethod = '/CreateSettlementMethod';
    return this.http.post(environment.apiUrl+ this.data+CreateSettlementMethod,data);
  }
  UpdateSettlementMethod(id:any,data:any){
    const UpdateSettlementMethod = '/UpdateSettlementMethod';
    return this.http.put(environment.apiUrl+ this.data+UpdateSettlementMethod,data,{params:{id}});
  }
  DeleteSettlementMethod(id:any){
    const DeleteSettlementMethod = '/DeleteSettlementMethod';
    return this.http.put(environment.apiUrl+ this.data+DeleteSettlementMethod,null,{params:{id}});
  }
  GetCustomerContactById(id:any){
    const GetCustomerContactById = '/GetCustomerContactById';
    return this.http.get(environment.apiUrl+ this.data+GetCustomerContactById,{params:{id}});
  }
  DeleteBatchCustomer(data:any){
    const DeleteBatchCustomer = '/DeleteBatchCustomer';
    return this.http.put(environment.apiUrl+ this.data+DeleteBatchCustomer,data);
  }
  CreateCollectionPeriod(data:any){
    const CreateCollectionPeriod = '/CreateCollectionPeriod';
    return this.http.post(environment.apiUrl+ this.data+CreateCollectionPeriod,data);
  }
  GetPagedCollectionPeriod(filter:{
    PageSize?:number,
    PageNumber?:number,
    Name?:string,
    SearchStatus?:string,
  }){
    const GetPagedCollectionPeriod = '/GetPagedCollectionPeriod';
    return this.http.get(environment.apiUrl+ this.data+GetPagedCollectionPeriod,{params:filter});
  }
  DeleteCollectionPeriod(id:any){
    const DeleteCollectionPeriod = '/DeleteCollectionPeriod';
    return this.http.put(environment.apiUrl+ this.data+DeleteCollectionPeriod,null,{params:{id}});
  }
  UpdateCollectionPeriod(id:any,data:any){
    const UpdateCollectionPeriod = '/UpdateCollectionPeriod';
    return this.http.put(environment.apiUrl+ this.data+UpdateCollectionPeriod,data,{params:{id}});
  }
  EnableCurrency(id:any){
    const EnableCurrency = '/EnableCurrency';
    return this.http.put(environment.apiUrl+ this.data+EnableCurrency,null,{params:{id}});
  }
  UnEnableCurrency(id:any){
    const UnEnableCurrency = '/UnEnableCurrency';
    return this.http.put(environment.apiUrl+ this.data+UnEnableCurrency,null,{params:{id}});
  }
  EnableSettlementMethod(id:any){
    const EnableSettlementMethod = '/EnableSettlementMethod';
    return this.http.put(environment.apiUrl+ this.data+EnableSettlementMethod,null,{params:{id}});
  }
  UnEnableSettlementMethod(id:any){
    const UnEnableSettlementMethod = '/UnEnableSettlementMethod';
    return this.http.put(environment.apiUrl+ this.data+UnEnableSettlementMethod,null,{params:{id}});
  }
  EnableCustomer(id:any){
    const EnableCustomer = '/EnableCustomer';
    return this.http.put(environment.apiUrl+ this.data+EnableCustomer,null,{params:{id}});
  }
  UnEnableCustomer(id:any){
    const UnEnableCustomer = '/UnEnableCustomer';
    return this.http.put(environment.apiUrl+ this.data+UnEnableCustomer,null,{params:{id}});
  }
  DownloadCustomerFile(){
    const DownloadCustomerFile = '/DownloadCustomerFile';
    return this.http.get(environment.apiUrl+ '/Excel'+DownloadCustomerFile,{responseType: 'blob'});
  }
  ImportCustomerFromExcel(file:File){
    const url = '/ImportCustomerFromExcel';
    const formData = new FormData();
    formData.append('file', file);
    return this.http.post(environment.apiUrl+ '/Excel'+url,formData);
  }
  GetLogsByCustomerId(filter:{
    Id?:number,
    PageNumber?:number,
    PageSize?:number,
  }){
    const GetLogsByCustomerId = '/GetLogsByCustomerId';
    return this.http.get(environment.apiUrl+ this.data+GetLogsByCustomerId,{params:filter});
  }
  GetAllCustomerLogs(filter:{
    PageNumber?:number,
    PageSize?:number,
  }){
    const GetAllCustomerLogs = '/GetAllCustomerLogs';
    return this.http.get(environment.apiUrl+ this.data+GetAllCustomerLogs,{params:filter});
  }
  CloseSaleOrderParts(data:any,state:boolean){
    return this.http.put(environment.apiUrl+'/Sale/CloseSaleOrderParts',data,{params:{close:state}})
  }
}
